import { Type } from 'class-transformer';
import { IsArray, IsBoolean, IsIn, IsInt, IsNotEmpty, IsNumber, IsObject, IsOptional, IsPositive, IsString, MaxLength, Min, ValidateNested } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

export class ProductSkuDTO {
  @ApiProperty({ description: 'SKU规格属性', example: { color: '黑色', size: 'XL' } })
  @IsObject({ message: '规格属性必须为对象格式' })
  @IsNotEmpty()
  specifications: Record<string, string>;

  @ApiProperty({ description: 'SKU价格', example: 299.9 })
  @IsNumber({}, { message: '价格必须为数字' })
  @IsPositive({ message: '价格必须大于0' })
  price: number;

  @ApiProperty({ description: '库存数量', example: 100 })
  @IsInt({ message: '库存必须为整数' })
  @Min(0, { message: '库存不能小于0' })
  stock: number;
}

export class CreateProductDTO {
  @ApiProperty({ description: '商品名称', example: '男士纯棉T恤' })
  @IsString()
  @IsNotEmpty()
  name: string;

  @ApiPropertyOptional({ description: '商品描述', example: '100%纯棉材质' })
  @IsString()
  description: string;

  @ApiProperty({ description: '基础价格', example: 199.9 })
  @IsNumber()
  @IsPositive()
  basePrice: number;

  @ApiProperty({
    type: [ProductSkuDTO],
    description: 'SKU列表',
    example: [{ specifications: { color: '黑色' }, price: 299.9, stock: 100 }]
  })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ProductSkuDTO)
  skus: ProductSkuDTO[];
  inventoryId: number;
}

export class UpdateProductDTO extends CreateProductDTO {
  @ApiProperty({ description: '商品ID', example: 1 })
  @IsNumber()
  @IsPositive()
  id: number;
}

export class ToggleProductStatusDTO {
  @ApiProperty({ description: '上架状态', example: true })
  @IsBoolean()
  isPublished: boolean;
}

export class UpdateSkuStockDTO {
  @ApiProperty({ description: '库存变动数量', example: 10 })
  @IsInt({ message: '数量必须为整数' })
  @Min(1, { message: '数量必须大于0' })
  quantity: number;

  @ApiProperty({
    enum: ['INBOUND', 'OUTBOUND'],
    description: '库存操作类型',
    example: 'INBOUND'
  })
  @IsIn(['INBOUND', 'OUTBOUND'], { message: '操作类型不合法' })
  operation: 'INBOUND' | 'OUTBOUND';

  @ApiPropertyOptional({
    description: '操作备注',
    example: '采购入库',
    maxLength: 200
  })
  @MaxLength(200, { message: '备注长度不能超过200字符' })
  @IsString()
  @IsOptional()
  remark?: string;
}